package com.caishi.lkx.exam.api;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caishi.lkx.common.AdminBaseApi;
import com.caishi.lkx.common.role.Role;
import com.caishi.lkx.common.role.Roles;
import com.caishi.lkx.exam.model.QuestionTypeModel;
import com.caishi.lkx.exam.service.IQuestionTypeService;
import com.caishi.lkx.user.model.UserModel;
import com.caishi.lkx.user.service.impl.UserServiceImpl;
import com.zzw.common.Result;
import com.zzw.common.Wrappers;

import com.caishi.lkx.exam.ExamResultCode;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author by Janisa
 * @Description: 考试规则相关
 * @date 2022/2/14 16:36
 */

@Tag(name = "考试规则相关接口")
@Slf4j
@RestController
@RequestMapping("/exam/question-type")
public class QuestionTypeApi extends AdminBaseApi<IQuestionTypeService, QuestionTypeModel, String> {

    @Resource
    private IQuestionTypeService questionTypeService;

    @Resource
    private UserServiceImpl userService;

    @Override
    public IQuestionTypeService loadService() {
        return questionTypeService;
    }

    @Override
    protected LambdaQueryWrapper<QuestionTypeModel> baseWrapper(QuestionTypeModel model, JSONObject otherParam) {
        return Wrappers.lambdaQuery(model.clearEmptyStringField("viewName", "remark"))
                .like(!CharSequenceUtil.isBlank(model.getName()), QuestionTypeModel::getName, "%" + model.getName() + "%")
                .like(!CharSequenceUtil.isBlank(model.getRemark()), QuestionTypeModel::getRemark, "%" + model.getRemark() + "%");
    }

    @Override
    @Roles({Role.admin})
    @GetMapping("/page")
    public IPage<QuestionTypeModel> page(@ModelAttribute Page<QuestionTypeModel> page, @ModelAttribute QuestionTypeModel model, @NotNull JSONObject otherParam) {
        IPage<QuestionTypeModel> data = loadService().page(loadService().pageProcessing(page), baseWrapper(model, otherParam));
        data.getRecords().parallelStream().forEach(this::setUserName);
        data.setRecords(listCover(data.getRecords(), otherParam));
        return data;
    }

    @Roles({Role.admin})
    @DeleteMapping("/deleted-list")
    public Result<String> deletedList(@RequestParam(value = "ids") List<String> ids) {
        Boolean delete = questionTypeService.delete(ids);
        return Boolean.TRUE.equals(delete) ? Result.success("删除成功") : Result.fail(ExamResultCode.deletedTypeFail);
    }

    private void setUserName(QuestionTypeModel model) {
        UserModel userDetail = userService.getUserDetail(model.getModifyBy());
        if (userDetail != null) {
            String userName = userDetail.getName();
            if (userName != null) {
                model.setFinalModifyUserName(userName);
            }
        }
    }
}
